package com.infolab.aims.web;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.infolab.aims.common.AbstractController;
import com.infolab.aims.common.bean.SearchCriteria;
import com.infolab.aims.common.bean.SearchResult;
import com.infolab.aims.domain.Archive;
import com.infolab.aims.domain.ArchivePK;
import com.infolab.aims.service.intf.ArchiveService;

@Controller
@RequestMapping("/archive")
@SessionAttributes(value = ArchiveController.SESSION_SEARCH_CRITERIA)
public class ArchiveController extends AbstractController {

	public static final String SESSION_SEARCH_CRITERIA = "searchCriteria";
	public static final String CONTEXT_PATH = "/archive";
	public static final String JSP_MAIN_SEARCH = "archiveSearch";
	Log logger = LogFactory.getLog(getClass());
	public static final Map<String, String> ARCHIVE_TYPE = new LinkedHashMap<String, String>();
	public static final Map<String, String> MODULE_ID = new LinkedHashMap<String, String>();

	@Autowired
	ArchiveService archiveService;

	@PostConstruct
	public void init() {
		ARCHIVE_TYPE.put("AssetModel", "Asset Model");
		ARCHIVE_TYPE.put("Location", "Location");
		ARCHIVE_TYPE.put("Section", "Section");
		ARCHIVE_TYPE.put("Department", "Department");
		ARCHIVE_TYPE.put("Vendor", "Vendor");
		
		MODULE_ID.put("AssetModel", "assetModelId");
		MODULE_ID.put("Location", "locationId");
		MODULE_ID.put("Section", "sectionId");
		MODULE_ID.put("Department", "departmentId");
		MODULE_ID.put("Vendor", "vendorId");
	}

	@RequestMapping(value = "search")
	public String searchArchive(@ModelAttribute(ArchiveController.SESSION_SEARCH_CRITERIA) SearchCriteria searchCriteria,
			@ModelAttribute("currentSearchCriteria") SearchCriteria currentSearchCriteria, Model model) {

		if (currentSearchCriteria.getCurPage() != null)
			searchCriteria.setCurPage(currentSearchCriteria.getCurPage());
		SearchResult searchResult = archiveService.searchArchive(searchCriteria);
		model.addAttribute(searchResult);
		model.addAttribute("types", ARCHIVE_TYPE);
		return JSP_MAIN_SEARCH;
	}

	@RequestMapping(value = "update/{id}/{type}", method = RequestMethod.GET)
	public String updateArchive(@PathVariable Integer id, @PathVariable String type, @ModelAttribute Archive archive, BindingResult result,
			Model model, HttpServletRequest request) {

		if (validateArchive(archive, result)) {
			setSuccessMessage(getMessageResources("success.error", "Archive id: " + id + ", type: " + type + " "));
			return "redirect:" + CONTEXT_PATH + "/" + JSP_MAIN_SEARCH;
		}

		ArchivePK archivePK = new ArchivePK(id, type);

		archiveService.updateArchive(archivePK);

		setSuccessMessage(getMessageResources("success.update", "Archive id: " + id + ", type: " + type + " "));

		return  "redirect:" + CONTEXT_PATH + "/search";

	}

	private boolean validateArchive(Archive archive, BindingResult result) {
		validator.validate(archive, result);
		return result.hasErrors();
	}

	@ModelAttribute(ArchiveController.SESSION_SEARCH_CRITERIA)
	public SearchCriteria getAuditSearchCriteria() {
		return new SearchCriteria();
	}
}
